<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="作业/试卷名称" prop="className">
        <el-input
          v-model="queryParams.testpaperName"
          placeholder="请输入作业/试卷名称"
          clearable
          size="small"
          style="width: 180px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布日期" prop="status">
        <el-date-picker
          v-model="queryParams.examDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="发布类型" prop="classDept">
        <el-select v-model="queryParams.pubilishType" clearable size="small" style="width: 180px">
          <el-option value="2" label="考试试卷"></el-option>
          <el-option value="1" label="日常作业"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="bclassList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column
        label="作业/试卷名称"
        align="center"
        prop="testpaperName"
        width="180"
        :show-overflow-tooltip="true"
      ></el-table-column>
      <el-table-column label="发布班级" align="center" prop="className" :show-overflow-tooltip="true"></el-table-column>
      <el-table-column
        label="发布类型"
        align="center"
        prop="pubilishType"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">
          <label v-if="scope.row.pubilishType==1">日常作业</label>
          <label v-if="scope.row.pubilishType==2">考试试卷</label>
        </template>
      </el-table-column>
      <!-- <el-table-column label="发布状态" align="center" prop="status" :show-overflow-tooltip="true" >
          <template slot-scope="scope">
            <label v-if="scope.row.status==1">有效</label>
            <label v-if="scope.row.status==0">无效</label>
          </template>
      </el-table-column>-->
      <el-table-column label="作业/试卷专业" align="center" prop="major" :show-overflow-tooltip="true">
        <template slot-scope="scope">{{ getMajorLable(scope.row.major) }}</template>
      </el-table-column>
      <el-table-column
        label="作业/试卷科目"
        align="center"
        prop="subjectId"
        :show-overflow-tooltip="true"
      >
        <template slot-scope="scope">{{ getSubjectLable(scope.row.subjectId) }}</template>
      </el-table-column>
      <el-table-column label="作业/试卷分类" align="center" prop="classify" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <label v-if="scope.row.classify==1">日常作业</label>
          <label v-if="scope.row.classify==2">考试试卷</label>
        </template>
      </el-table-column>
      <el-table-column label="作业/试卷总分" align="center" prop="totalPoints" width="160"></el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">{{ parseTime(scope.row.createTime) }}</template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="createBy" width="160"></el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="撤销发布" placement="top">
            <el-button
              size="mini"
              circle
              type="warning"
              icon="el-icon-refresh-left"
              @click="handleRefresh(scope.row)"
            />
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="查看考试学生" placement="top">
            <el-button size="mini" circle icon="el-icon-view" @click="viewStudent(scope.row)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!--试卷发布信息填写-->
    <el-dialog title="试题发布" :visible.sync="publishAperVisible" width="400px">
      <el-form :model="form" ref="form" :rules="rules" label-width="110px">
        <el-form-item label="发布班级" prop="classId">
          <el-select v-model="form.classId">
            <el-option
              v-for="item in classOptions"
              :key="item.id"
              :label="item.className"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布类型" prop="pubilishType">
          <el-select v-model="form.pubilishType">
            <el-option value="1" label="日常作业"></el-option>
            <el-option value="2" label="考试试卷"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPublish">确 定</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import { findSchoolClassPageList } from "@/api/classManage/classInfo";
import { findBaseSpaceLocationPageList } from "@/api/baseData/siteManagement";
import { getClassTeacherList } from "@/api/system/user";
import { findBaseSubjectPageList } from "@/api/baseData/profession";
import { listTermtime } from "@/api/system/termtime.js";
import { queryPublish, cancelPublish } from "@/api/studentInfo/examPublish";
import { subjectList } from "@/api/educational/subject.js";

export default {
  name: "BClass",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      rowList: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      stuCount: 0,
      // 字典表格数据
      bclassList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 班主任
      userOptions: [],
      // 状态数据字典
      statusOptions: [],
      // 班级所属部门
      classDeptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        className: undefined,
        status: undefined,
        classDept: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        classId: [
          { required: true, message: "考试名称不能为空", trigger: "blur" }
        ],
        pubilishType: [
          { required: true, message: "考试开始时间不能为空", trigger: "blur" }
        ]
      },
      subjectList: [],
      majorList: [], //专业
      termTimeList: [],
      courseTypeOptions: [],
      classOptions: [],
      classGradeList: [],
      publishAperVisible: false
    };
  },
  created() {
    this.getMajortList();
    this.getList();
    this.getClassTeacherList();
    this.getSubjectList();
    this.getTermTimeList();
    this.getClassList();

    this.getDicts("class_grade").then(response => {
      //年级
      this.classGradeList = response.data;
    });
  },
  methods: {
    handleRefresh(row) {
      //撤销发布操作
      this.$confirm("是否撤销已发布的试题?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          cancelPublish(row.id).then(resp => {
            this.getList();
            this.msgSuccess("撤销发布成功");
          });
        })
        .catch(function() {});
    },
    viewStudent(row) {
      this.$router.push({
        path: "/educational/exam/examStudent",
        query: { id: row.id }
      });
    },
    getClassRoom() {
      findBaseSpaceLocationPageList({
        pageNum: 1,
        pageSize: 1000,
        locationType: 4
      }).then(response => {
        this.classRoomOptions = response.data.list;
      });
    },
    getClassName(id) {
      this.classOptions.forEach(item => {
        if (item.id == id) {
          return item.className;
        }
      });
    },
    getClassList() {
      //查询班级
      findSchoolClassPageList({
        pageSize: 500,
        pageNum: 1,
        status: 1,
        isGraduation: 0
      }).then(resp => {
        this.classOptions = resp.data.list;
      });
    },
    getTermTimeList() {
      //查询学期
      this.loading = true;
      listTermtime({ pageSize: 100, pageNum: 1, status: 1 }).then(response => {
        this.termTimeList = response.data.list;
      });
    },
    getMajortList() {
      //查询科目
      findBaseSubjectPageList({ pageSize: 100, pageNum: 1, status: 1 }).then(
        response => {
          this.majorList = response.data.list;
        }
      );
    },
    getSubjectList() {
      //查询专柜
      subjectList({ pageSize: 100, pageNum: 1, status: 1 }).then(response => {
        this.subjectList = response.data.list;
      });
    },
    getMajorLable(val) {
      for (let i = 0; i < this.majorList.length; i++) {
        if (this.majorList[i].id == val) {
          return this.majorList[i].subjectName;
        }
      }
    },
    getSubjectLable(val) {
      for (let i = 0; i < this.subjectList.length; i++) {
        if (this.subjectList[i].id == val) {
          return this.subjectList[i].subjName;
        }
      }
    },
    /** 查询班级列表 */
    getList() {
      this.loading = true;
      queryPublish(this.queryParams).then(response => {
        this.bclassList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    getClassTeacherList() {
      getClassTeacherList().then(response => {
        this.userOptions = response.data;
      });
    },
    // 字典设备状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 字典设备状态字典翻译
    classDeptFormat(row, column) {
      return this.selectDictLabel(this.classDeptOptions, row.classDept);
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        className: undefined,
        classTeacherId: undefined,
        status: "1",
        classDept: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/educational/exam/editPaper");
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.log(selection);
      this.rowList = selection;

      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        path: "/educational/exam/editPaper",
        query: { id: row.id }
      });
    },
    submitPublish() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          publishTestPaper(this.form).then(resp => {
            this.msgSuccess("发布成功");
            this.publishAperVisible = false;
          });
        }
      });
    },
    handleClose() {
      this.publishAperVisible = false;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return deleteTestPaper(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },

    // 获取当前选中班主任的名称
    selectUser(val) {
      alert(val.label);
      this.form.username = val.label;
    }
  }
};
</script>
  